package com.hxc.shop.service;

import com.hxc.shop.bean.ShopGoodsBuyRecord;
import com.hxc.shop.bean.ShopGoodsComment;
import com.hxc.shop.common.ResultCode;
import com.hxc.shop.common.ResultVO;
import com.hxc.shop.mapper.Mysql.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class CommentService {
    @Resource
    private CommentMapper commentMapper;
    @Resource
    private GoodsOrderMapper orderMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private GoodsInfoMapper goodsInfoMapper;

    public ResultVO addComment(ShopGoodsComment comment) {
        if (comment == null) {
            return new ResultVO(ResultCode.ERROR.getCode(),"评价数据为空，评价商品失败!");
        }
        if (StringUtils.isEmpty(comment.getUserName())) {
            return new ResultVO(ResultCode.ERROR.getCode(),"评价用户名为空,评价商品失败!");
        }
        if (StringUtils.isEmpty(comment.getGoodsId())) {
            return new ResultVO(ResultCode.ERROR.getCode(),"评价商品编号为空，评价商品失败!");
        }
        if (StringUtils.isEmpty(comment.getGoodsComment())) {
            return new ResultVO(ResultCode.ERROR.getCode(),"评价内容为空,评价商品失败!");
        }
        if (StringUtils.isEmpty(comment.getGoodsRating())) {
            return new ResultVO(ResultCode.ERROR.getCode(),"商品评分为空,评价商品失败!");
        }
        // 查找是否有商品编号
        Integer checkGoodsId = 0;
        try {
            checkGoodsId = goodsInfoMapper.findGoodsIdByGoodsId(comment.getGoodsId());
        }catch (Exception e) {
            log.error("新增评论查找商品编号失败:"+comment.getGoodsId()+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找商品编号失败:"+e.getMessage());
        }
        if (checkGoodsId == null || checkGoodsId == 0) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此商品编号,新增评论失败!");
        }
        // 查找是否有该用户名
        String checkUserName ="";
        try {
            checkUserName = userMapper.findUserNameByUserName(comment.getUserName());
        }catch (Exception e) {
            log.error("查找用户名失败:"+comment.getUserName()+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找用户名失败:"+e.getMessage());
        }
        if (StringUtils.isEmpty(checkUserName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此用户,添加评论失败!");
        }

        // 判断该用户是否有购买该商品，有购买才评价
        List<ShopGoodsBuyRecord> shopGoodsBuyRecordList = new ArrayList<>();
        try {
            shopGoodsBuyRecordList = orderMapper.getBuyRecordByUserName(comment.getUserName());
        }catch (Exception e) {
            log.error("查找购买商品记录失败,评价失败:"+comment+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找购买商品记录失败,评价失败:"+e.getMessage());
        }
        if (shopGoodsBuyRecordList.size()==0) {
            return new ResultVO(ResultCode.ERROR.getCode(),"您未购买该商品，禁止评价!");
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        comment.setCreateTime(sdf.format(new Date()));
        comment.setUpdateTime(sdf.format(new Date()));
        try {
            commentMapper.addComment(comment);
        }catch (Exception e) {
            log.error("插入商品评价失败:"+comment+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"评价商品失败："+e.getMessage());
        }
        return new ResultVO(ResultCode.SUCCESS.getCode(),"评价商品成功!");
    }

    public ResultVO findCommentByUserName(String userName) {
        if (StringUtils.isEmpty(userName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"用户名为空,获取商品评价失败!");
        }
        // 查找是否有该用户名
        String checkUserName ="";
        try {
            checkUserName = userMapper.findUserNameByUserName(userName);
        }catch (Exception e) {
            log.error("查找用户名失败:"+userName+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找用户名失败:"+e.getMessage());
        }
        if (StringUtils.isEmpty(checkUserName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此用户,获取评论失败!");
        }
        List<ShopGoodsComment> goodsCommentList = new ArrayList<>();
        try {
            goodsCommentList = commentMapper.findCommentByUserName(userName);
        }catch (Exception e) {
            log.error("获取商品评价失败:"+userName+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"获取商品评价失败:"+e.getMessage());
        }
        return new ResultVO(ResultCode.SUCCESS,goodsCommentList);
    }

    public ResultVO findCommentByGoodsId(Integer goodsId) {
        if (StringUtils.isEmpty(goodsId)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"商品编号为空,获取商品评价失败!");
        }
        // 查找是否有商品编号
        Integer checkGoodsId = 0;
        try {
            checkGoodsId = goodsInfoMapper.findGoodsIdByGoodsId(goodsId);
        }catch (Exception e) {
            log.error("查找评论查询商品编号失败:"+goodsId+"=="+e.getMessage());
        }
        if (checkGoodsId == null || checkGoodsId == 0) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此商品编号,获取评论失败!");
        }
        List<ShopGoodsComment> goodsCommentList = new ArrayList<>();
        try {
            goodsCommentList = commentMapper.findCommentByGoodsId(goodsId);
        }catch (Exception e) {
            log.error("获取商品评价失败:"+goodsId+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"获取商品评价失败:"+e.getMessage());
        }
        return new ResultVO(ResultCode.SUCCESS,goodsCommentList);
    }


    public ResultVO findCommentByGoodsIdAndUserName(Integer goodsId,String userName) {
        if (StringUtils.isEmpty(goodsId)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"商品编号为空,获取商品评价失败!");
        }
        if (StringUtils.isEmpty(userName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"用户名为空,获取商品评价失败!");
        }
        // 查找是否有商品编号
        Integer checkGoodsId = 0;
        try {
            checkGoodsId = goodsInfoMapper.findGoodsIdByGoodsId(goodsId);
        }catch (Exception e) {
            log.error("查找评论查询商品编号失败:"+goodsId+"=="+e.getMessage());
        }
        if (checkGoodsId == null || checkGoodsId == 0) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此商品编号,获取评论失败!");
        }
        // 查找是否有该用户名
        String checkUserName ="";
        try {
            checkUserName = userMapper.findUserNameByUserName(userName);
        }catch (Exception e) {
            log.error("查找用户名失败:"+userName+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找用户名失败:"+e.getMessage());
        }
        if (StringUtils.isEmpty(checkUserName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此用户,获取评论失败!");
        }
        List<ShopGoodsComment> goodsCommentList = new ArrayList<>();
        try {
            goodsCommentList = commentMapper.findCommentByGoodsIdAndUserName(goodsId,userName);
        }catch (Exception e) {
            log.error("获取商品评价失败:"+goodsId+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"获取商品评价失败:"+e.getMessage());
        }
        return new ResultVO(ResultCode.SUCCESS,goodsCommentList);
    }

    public ResultVO deleteCommentById(Integer id) {
        if (StringUtils.isEmpty(id)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"评论编号为空,删除商品评价失败!");
        }
        // 查找是否有商品编号
        Integer checkGoodsId = 0;
        try {
            checkGoodsId = goodsInfoMapper.findGoodsIdByGoodsId(id);
        }catch (Exception e) {
            log.error("查找评论查询商品编号失败:"+id+"=="+e.getMessage());
        }
        if (checkGoodsId == null || checkGoodsId == 0) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此商品编号,删除评论失败!");
        }
        try {
            commentMapper.deleteCommentById(id);
        }catch (Exception e) {
            log.error("删除商品评价失败:"+id+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"删除商品评价失败:"+e.getMessage());
        }
        return new ResultVO(ResultCode.ERROR.getCode(),"删除商品评价成功!评论编号:"+id);
    }
}

